package com.teacher.service.listener;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.teacher.pojo.entity.PhysicalEducationMajor;
import com.teacher.pojo.entity.PhysicalEducationNormal;
import com.teacher.pojo.excel.PhysicalEducationMajorExcel;
import com.teacher.pojo.excel.PhysicalEducationNormalExcel;
import com.teacher.service.IPerStudentService;
import com.teacher.service.IPhysicalEducationMajorService;
import com.teacher.service.IPhysicalEducationNormalService;
import org.springframework.beans.BeanUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author: zfm
 * @Date: 2021/7/30 16:17
 */
public class ExcelPhysicalEducationMajorListener extends AnalysisEventListener<PhysicalEducationMajorExcel> {
    private Long termId;
    private IPhysicalEducationMajorService iPhysicalEducationMajorService;

    private List<PhysicalEducationMajor> physicalEducationMajorList = new ArrayList<>();
    private IPerStudentService iPerStudentService;

    public ExcelPhysicalEducationMajorListener(Long termId, IPhysicalEducationMajorService iPhysicalEducationMajorService, IPerStudentService iPerStudentService) {
        this.termId = termId;
        this.iPhysicalEducationMajorService = iPhysicalEducationMajorService;
        this.iPerStudentService = iPerStudentService;
    }

    @Override
    public void invoke(PhysicalEducationMajorExcel physicalEducationMajorExcel, AnalysisContext analysisContext) {
        if (iPerStudentService.isExit(physicalEducationMajorExcel.getStudentKey())) {
            PhysicalEducationMajor physicalEducationMajor = new PhysicalEducationMajor();
            BeanUtils.copyProperties(physicalEducationMajorExcel, physicalEducationMajor);
            physicalEducationMajor.setTermId(termId);
            physicalEducationMajorList.add(physicalEducationMajor);
        }
    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext analysisContext) {
        saveData();
    }

    private void saveData() {
        iPhysicalEducationMajorService.saveBatch(physicalEducationMajorList);
    }
}
